﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Html.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

#endregion

namespace IveelySE.Common.Html
{
    /// <summary>
    ///   HTML分析
    /// </summary>
    public class Html
    {
        /// <summary>
        ///   当前分析的URL
        /// </summary>
        private static string _url;

        /// <summary>
        ///   网页数据
        /// </summary>
        private static string _webData;

        /// <summary>
        ///   下载网页源码进程
        /// </summary>
        private static Thread _process;

        /// <summary>
        ///   获取HTML源码(暂时不能提取PDF等其它文档格式内容)
        /// </summary>
        public static string GetSource(string url)
        {
            _url = url;
            _process = new Thread(GetHtml);
            _process.Start();
            return _webData;
        }

        /// <summary>
        ///   获取网页内容(暂时不支持广告过滤)
        /// </summary>
        /// <param name="source"> 网页源码 </param>
        /// <returns> 返回内容 </returns>
        public static string GetContent(string source)
        {
            try
            {
                string strallstrm = source;
                var reg = new Regex(@"[^([\x00-\x7F]|[\x80-\xFE][\x40-\x7E\x80-\xFE]]",
                                    RegexOptions.None | RegexOptions.IgnoreCase | RegexOptions.Multiline);

                // var reg = new Regex("(? <= <li> <a[^>]*>).*?(?= </a> </li>)"); 
                MatchCollection mc = reg.Matches(strallstrm);
                string result = string.Empty;
                for(int i = 0; i < mc.Count; i++)
                {
                    result += mc[i].ToString();
                }
                return result;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        ///   获取网页标题
        /// </summary>
        /// <param name="source"> 网页源码 </param>
        /// <returns> 返回标题 </returns>
        public static string GetTilte(string source)
        {
            try
            {
                string strWebText = source;
                int i1;
                int i2;
                if(strWebText.IndexOf("<title>", StringComparison.Ordinal) < 0)
                {
                    i1 = strWebText.IndexOf("<TITLE>", StringComparison.Ordinal) + 7;
                    i2 = strWebText.IndexOf("</TITLE>", StringComparison.Ordinal);
                }
                else
                {
                    i1 = strWebText.IndexOf("<title>", StringComparison.Ordinal) + 7;
                    i2 = strWebText.IndexOf("</title>", StringComparison.Ordinal);
                }

                string strTitle = strWebText.Substring(i1, i2 - i1);
                return strTitle;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        ///   获取网页信息
        /// </summary>
        private static void GetHtml()
        {
            try
            {
                string strWebData;
                using(var myWebClient = new WebClient())
                {
                    byte[] myDataBuffer = myWebClient.DownloadData(_url);
                    strWebData = Encoding.Default.GetString(myDataBuffer);

                    //获取网页字符编码描述信息 
                    Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"",
                                                     RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    string webCharSet = charSetMatch.Groups[2].Value.Replace("\"", string.Empty);
                    webCharSet = webCharSet == string.Empty ? "UTF-8" : webCharSet;
                    if(!Equals(Encoding.GetEncoding(webCharSet), Encoding.Default))
                    {
                        strWebData = Encoding.GetEncoding(webCharSet).GetString(myDataBuffer);
                    }
                }

                _webData = strWebData;
            }
            catch
            {
                _webData = string.Empty;
            }
        }
    }
}